import bs4
import re

from bs4 import BeautifulSoup
import re


#bs4、re练习Demo
html="""
<html>
<head><title >story12345</title></head>
<body>
<p class="title" name="dromouse"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1"><span>westos</span><!-- Elsie --></a>,
<a href="http://example.com/lacie" class="sister1" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
<p>story</P>
"""
#自动补齐HTML的标签
soup=BeautifulSoup(html,"html.parser")
print(soup.prettify())

#标签常用属性（只能获取头一个标签的属性内容）
print(soup.title)
print(type(soup.title))   #类型
print(soup.title.name)    #标签名称
print(soup.a.attrs)   #返回标签内的属性信息
print(soup.a.attrs['class'])
print(soup.a.attrs['id'])     #返回的为字典
print(soup.a.attrs['href'])


#标签常用方法
print(soup.a.get('href'))  #用于得到标签下的属性值
print(soup.a.get('class'))

print(soup.a.string)  #一个标签或没有标签的情况下，返回标签下的文本内容（注释也算一个标签）
print(soup.a.get_text()) #标签下的文本内容，包括子节点


#对获取的属性进行修改
print(soup.a.get('href'))
soup.a['href']="www.baidu.com"
print(soup.a.get('href'))

#面向对象的匹配
#1.查找符合值的所有标签
TapAll=soup.find_all('a')
# for i in TapAll:
#     print(i)
#2.查找符合值且类名相等的所有标签
TapAll2=soup.find_all('a',class_="sister")
# for i in TapAll2:
#     print(i)

# 根据内容进行匹配
#1.根据文本匹配print(soup.find_all(text='story'))
#2.根据正则匹配
print(soup.find_all(text=re.compile(r'.*?story.*?')))

